﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Windows.Forms;

namespace TMS
{
    class Mail
    {
        private MailAddress FromAddres, ToAddress;
        private string password;
        private string Subject;
        private string Body;
        private SmtpClient Server = new SmtpClient();

        public Mail()
        {
            
        }

        public void SetServer()
        {
            Server.Host = "mail.tehnoinspect.com.mk";
            FromAddres = new MailAddress("tms@tehnoinspect.com.mk", "TMS");
            password = "tmstehnoins";
            //Server.EnableSsl = true;
            Server.Port = 587;
            Server.DeliveryMethod = SmtpDeliveryMethod.Network;
            Server.UseDefaultCredentials = false;
            Server.Credentials = new NetworkCredential(FromAddres.Address, password);
        }

        public void SendTestMail()
        {
            SetServer();
            ToAddress = new MailAddress("goranvladev@gmail.com", "Goran Vladev");
            MailMessage NewMesage = new MailMessage(FromAddres, ToAddress);
            NewMesage.Subject = "test";
            NewMesage.Body = "Ova e test poraka";
            Server.Send(NewMesage);
            
        }

        public void SendForProductOrder(Int32 productID, Int32 WarehouseQuantity)
        {
            SetServer();
            Product product = new Product();
            DataSet prod = new DataSet();
            prod=product.GetProductFromDBAsDataSet(productID);
            ToAddress = new MailAddress("nabavka@tehnoinspect.com.mk", "Набавки");
            MailMessage NewMesage = new MailMessage(FromAddres, ToAddress);
            NewMesage.Subject = "TMS - Информација за помината минимална залиха";
            NewMesage.Body = "Производ број:\t\t"+prod.Tables[0].Rows[0].ItemArray[5].ToString()+"\n"+
                "Име на производ:\t\t" + prod.Tables[0].Rows[0].ItemArray[4].ToString() + "\n" +
                "Опис:\t\t\t\t" + prod.Tables[0].Rows[0].ItemArray[10].ToString() + "\n" +
                "Минимална залиха:\t\t" + prod.Tables[0].Rows[0].ItemArray[9].ToString() + " " + prod.Tables[0].Rows[0].ItemArray[11].ToString() + "\n" +
                "Моментална количина:\t "+WarehouseQuantity+ " " + prod.Tables[0].Rows[0].ItemArray[11].ToString()+"\n";
            Server.Send(NewMesage);

        }

        public void SendSupprtMail(string e)
        {
            SetServer();
            ToAddress = new MailAddress("tms_support@tehnoinspect.com.mk", "Грешка");
            MailMessage NewMesage = new MailMessage(FromAddres, ToAddress);
            NewMesage.Subject = "TMS - Error report";
            NewMesage.Body = e;
            Server.Send(NewMesage);
            MessageBox.Show("Ситемска грешка \nПратена е порака до администраторот на TMS");

        }
        
    }
}
